home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dejagnu.lha / dejagnu-1.0.1 / tcl / doc / Hash.3 < prev    next >
Text File  |  1993-02-14  |  8KB  |  210 lines

  1. '\"
  2. '\" Copyright 1989 Regents of the University of California
  3. '\" Permission to use, copy, modify, and distribute this
  4. '\" documentation for any purpose and without fee is hereby
  5. '\" granted, provided that this notice appears in all copies.
  6. '\" The University of California makes no representations about
  7. '\" the suitability of this material for any purpose.  It is
  8. '\" provided "as is" without express or implied warranty.
  9. '\" 
  10. .so man.macros
  11. .HS Tcl_Hash tcl
  12. .BS
  13. .SH NAME
  14. .na
  15. Tcl_InitHashTable, Tcl_DeleteHashTable, Tcl_CreateHashEntry, Tcl_DeleteHashEntry, Tcl_FindHashEntry, Tcl_GetHashValue, Tcl_SetHashValue, Tcl_GetHashKey, Tcl_FirstHashEntry, Tcl_NextHashEntry, Tcl_HashStats \- procedures to manage hash tables
  16. .SH SYNOPSIS
  17. .nf
  18. \fB#include <tclHash.h>\fR
  19. .sp
  20. \fBTcl_InitHashTable\fR(\fItablePtr, keyType\fR)
  21. .sp
  22. \fBTcl_DeleteHashTable\fR(\fItablePtr\fR)
  23. .sp
  24. Tcl_HashEntry *
  25. \fBTcl_CreateHashEntry\fR(\fItablePtr, key, newPtr\fR)
  26. .sp
  27. \fBTcl_DeleteHashEntry\fR(\fIentryPtr\fR)
  28. .sp
  29. Tcl_HashEntry *
  30. \fBTcl_FindHashEntry\fR(\fItablePtr, key\fR)
  31. .sp
  32. ClientData
  33. \fBTcl_GetHashValue\fR(\fIentryPtr\fR)
  34. .sp
  35. \fBTcl_SetHashValue\fR(\fIentryPtr, value\fR)
  36. .sp
  37. char *
  38. \fBTcl_GetHashKey\fR(\fItablePtr, entryPtr\fR)
  39. .sp
  40. Tcl_HashEntry *
  41. \fBTcl_FirstHashEntry\fR(\fItablePtr, searchPtr\fR)
  42. .sp
  43. Tcl_HashEntry *
  44. \fBTcl_NextHashEntry\fR(\fIsearchPtr\fR)
  45. .sp
  46. char *
  47. \fBTcl_HashStats\fR(\fItablePtr\fR)
  48. .SH ARGUMENTS
  49. .AS Tcl_HashSearch *searchPtr
  50. .AP Tcl_HashTable *tablePtr in
  51. Address of hash table structure (for all procedures but
  52. \fBTcl_InitHashTable\fR, this must have been initialized by
  53. previous call to \fBTcl_InitHashTable\fR).
  54. .AP int keyType in
  55. Kind of keys to use for new hash table.  Must be either
  56. TCL_STRING_KEYS, TCL_ONE_WORD_KEYS, or an integer value
  57. greater than 1.
  58. .AP char *key in
  59. Key to use for probe into table.  Exact form depends on
  60. \fIkeyType\fR used to create table.
  61. .AP int *newPtr out
  62. The word at \fI*newPtr\fR is set to 1 if a new entry was created
  63. and 0 if there was already an entry for \fIkey\fR.
  64. .AP Tcl_HashEntry *entryPtr in
  65. Pointer to hash table entry.
  66. .AP ClientData value in
  67. New value to assign to hash table entry.  Need not have type
  68. ClientData, but must fit in same space as ClientData.
  69. .AP Tcl_HashSearch *searchPtr in
  70. Pointer to record to use to keep track of progress in enumerating
  71. all the entries in a hash table.
  72. .BE
  73.  
  74. .SH DESCRIPTION
  75. .PP
  76. A hash table consists of zero or more entries, each consisting of
  77. a key and a value.
  78. Given the key for an entry, the hashing routines can very quickly
  79. locate the entry, and hence its value.
  80. There may be at most one entry in a hash table with a
  81. particular key, but many entries may have the same value.
  82. Keys can take one of three forms:  strings,
  83. one-word values, or integer arrays.
  84. All of the keys in a given table have the same form, which is
  85. specified when the table is initialized.
  86. .PP
  87. The value of a hash table entry can be anything that fits in
  88. the same space as a ``char *'' pointer.
  89. Values for hash table entries are managed entirely by clients,
  90. not by the hash module itself.
  91. Typically each entry's value is a pointer to a data structure
  92. managed by client code.
  93. .PP
  94. Hash tables grow gracefully as the number of entries increases,
  95. so that there are always less than three entries per hash bucket,
  96. on average.
  97. This allows for fast lookups regardless of the number of entries
  98. in a table.
  99. .PP
  100. \fBTcl_InitHashTable\fR initializes a structure that describes
  101. a new hash table.
  102. The space for the structure is provided by the caller, not by
  103. the hash module.
  104. The value of \fIkeyType\fR indicates what kinds of keys will
  105. be used for all entries in the table.  \fIKeyType\fR must have
  106. one of the following values:
  107. .IP \fBTCL_STRING_KEYS\fR 25
  108. Keys are null-terminated ASCII strings.
  109. They are passed to hashing routines using the address of the
  110. first character of the string.
  111. .IP \fBTCL_ONE_WORD_KEYS\fR 25
  112. Keys are single-word values;  they are passed to hashing routines
  113. and stored in hash table entries as ``char *'' values.
  114. The pointer value is the key;  it need not (and usually doesn't)
  115. actually point to a string.
  116. .IP \fIother\fR 25
  117. If \fIkeyType\fR is not TCL_STRING_KEYS or TCL_ONE_WORD_KEYS,
  118. then it must be an integer value greater than 1.
  119. In this case the keys will be arrays of ``int'' values, where
  120. \fIkeyType\fR gives the number of ints in each key.
  121. This allows structures to be used as keys.
  122. All keys must have the same size.
  123. Array keys are passed into hashing functions using the address
  124. of the first int in the array.
  125. .PP
  126. \fBTcl_DeleteHashTable\fR deletes all of the entries in a hash
  127. table and frees up the memory associated with the table's
  128. bucket array and entries.
  129. It does not free the actual table structure (pointed to
  130. by \fItablePtr\fR), since that memory is assumed to be managed
  131. by the client.
  132. \fBTcl_DeleteHashTable\fR also does not free or otherwise
  133. manipulate the values of the hash table entries.
  134. If the entry values point to dynamically-allocated memory, then
  135. it is the client's responsibility to free these structures
  136. before deleting the table.
  137. .PP
  138. \fBTcl_CreateHashEntry\fR locates the entry corresponding to a
  139. particular key, creating a new entry in the table if there
  140. wasn't already one with the given key.
  141. If an entry already existed with the given key then \fI*newPtr\fR
  142. is set to zero.
  143. If a new entry was created, then \fI*newPtr\fR is set to a non-zero
  144. value and the value of the new entry will be set to zero.
  145. The return value from \fBTcl_CreateHashEntry\fR is a pointer to
  146. the entry, which may be used to retrieve and modify the entry's
  147. value or to delete the entry from the table.
  148. .PP
  149. \fBTcl_DeleteHashEntry\fR will remove an existing entry from a
  150. table.
  151. The memory associated with the entry itself will be freed, but
  152. the client is responsible for any cleanup associated with the
  153. entry's value, such as freeing a structure that it points to.
  154. .PP
  155. \fBTcl_FindHashEntry\fR is similar to \fBTcl_CreateHashEntry\fR
  156. except that it doesn't create a new entry if the key doesn't exist;
  157. instead, it returns NULL as result.
  158. .PP
  159. \fBTcl_GetHashValue\fR and \fBTcl_SetHashValue\fR are used to
  160. read and write an entry's value, respectively.
  161. Values are stored and retrieved as type ``ClientData'', which is
  162. large enough to hold a pointer value.  On almost all machines this is
  163. large enough to hold an integer value too.
  164. .PP
  165. \fBTcl_GetHashKey\fR returns the key for a given hash table entry,
  166. either as a pointer to a string, a one-word (``char *'') key, or
  167. as a pointer to the first word of an array of integers, depending
  168. on the \fIkeyType\fR used to create a hash table.
  169. In all cases \fBTcl_GetHashKey\fR returns a result with type
  170. ``char *''.
  171. When the key is a string or array, the result of \fBTcl_GetHashKey\fR
  172. points to information in the table entry;  this information will
  173. remain valid until the entry is deleted or its table is deleted.
  174. .PP
  175. \fBTcl_FirstHashEntry\fR and \fBTcl_NextHashEntry\fR may be used
  176. to scan all of the entries in a hash table.
  177. A structure of type ``Tcl_HashSearch'', provided by the client,
  178. is used to keep track of progress through the table.
  179. \fBTcl_FirstHashEntry\fR initializes the search record and
  180. returns the first entry in the table (or NULL if the table is
  181. empty).
  182. Each susequent call to \fBTcl_NextHashEntry\fR returns the
  183. next entry in the table or
  184. NULL if the end of the table has been reached.
  185. A call to \fBTcl_FirstHashEntry\fR followed by calls to
  186. \fBTcl_NextHashEntry\fR will return each of the entries in
  187. the table exactly once, in an arbitrary order.
  188. It is unadvisable to modify the structure of the table, e.g.
  189. by creating or deleting entries, while the search is in
  190. progress.
  191. .PP
  192. \fBTcl_HashStats\fR returns a dynamically-allocated string with
  193. overall information about a hash table, such as the number of
  194. entries it contains, the number of buckets in its hash array,
  195. and the utilization of the buckets.
  196. It is the caller's responsibility to free the result string
  197. by passing it to \fBfree\fR.
  198. .PP
  199. The header file \fBtclHash.h\fR defines the actual data structures
  200. used to implement hash tables.
  201. This is necessary so that clients can allocate Tcl_HashTable
  202. structures and so that macros can be used to read and write
  203. the values of entries.
  204. However, users of the hashing routines should never refer directly
  205. to any of the fields of any of the hash-related data structures;
  206. use the procedures and macros defined here.
  207.  
  208. .SH KEYWORDS
  209. hash table, key, lookup, search, value
  210.